package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    //刘贤斌 分页查询菜单
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return menuService.findPage(queryPageBean);
    }
    @RequestMapping("/getSonMenuByID")
    public Result getSonMenuByID(Integer id){
        List<Menu> menuList= menuService.getSonMenuByID(id);
         return  new Result(true,"success",menuList)  ;
    }
    //刘贤斌 获取所以Menu 为下拉框赋值
    @RequestMapping("/getAllMenu")
    public Result getAllMenu(){
        List<Menu> menuList= menuService.getAllMenu();
        return  new Result(true,"success",menuList)  ;
    }
    //刘贤斌 新增菜单
    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu){
        try{
            menuService.add(menu);
            return new Result(true,"success");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"fail");
        }
    }
    //刘贤斌 编译菜单
    @RequestMapping("/edit")
    public Result edit(@RequestBody Menu Menu){
        try{
            menuService.edit(Menu);
            return new Result(true,"sucess");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"fail");
        }
    }
    //刘贤斌 根据id删除menu
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            menuService.delete(id);//发送请求
            return new Result(true,"删除菜单成功");
        }catch (Exception e){
            String message = e.getMessage();
            e.printStackTrace();
            return new Result(false, message);
        }
    }
    //刘贤斌  根据用户名所拥有的权限展示其对应的菜单
    @RequestMapping("/findMenuByName")
    public Result findMenuByName(String name){
        try{
        List<Menu> menuList=menuService.findMenuByName(name);
            return new Result(true,"success",menuList);
        }catch (Exception e){
            String message = e.getMessage();
            e.printStackTrace();
            return new Result(false, message);
        }
    }

}
